﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Weibo4Sharp.Service;
using System.Net;
using NUnit.Framework;
using Weibo4Sharp.Model;

namespace Weibo4SharpTest.Service
{
    public abstract class WeiboServiceTestBase
    {
        protected static readonly string _hero;
        protected static readonly string _consumerKey;
        protected static readonly string _consumerSecret;
        protected static readonly string _accessToken;
        protected static readonly string _accessTokenSecret;

        static WeiboServiceTestBase()
        {
            _hero = ConfigurationManager.AppSettings["Hero"];
            _consumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
            _consumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"];
            _accessToken = ConfigurationManager.AppSettings["AccessToken"];
            _accessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecret"];
        }

        protected static void AssertResultWas(WeiboService service, HttpStatusCode statusCode)
        {
            Assert.IsNotNull(service.Response);
            if (statusCode != service.Response.StatusCode)
            {
                Console.WriteLine(service.Response.Response);
            }
            Assert.AreEqual(statusCode, service.Response.StatusCode);
        }

        protected static void AssertResultWasOK(WeiboService service)
        {
            AssertResultWas(service, HttpStatusCode.OK);
        }

        protected WeiboService GetAuthenticatedService()
        {
            var service = new WeiboService(
                _consumerKey,
                _consumerSecret,
                _accessToken,
                _accessTokenSecret
                );
           
            return service;
        }

        protected WeiboService GetService()
        {
            var service = new WeiboService(_consumerKey, _consumerSecret);

            return service;
        }

        protected void PrintUser(WeiboUser user)
        {
            Console.WriteLine("id:{0} screenName:{1} friends:{2} followers:{3} statuses:{4}",
                user.Id, user.ScreenName, user.FriendsCount, user.FollowersCount, user.StatusesCount);
        }

        protected void PrintWeibos(IEnumerable<WeiboStatus> weibos)
        {
            foreach (var weibo in weibos)
            {
                Assert.AreNotEqual(0, weibo.Id);
                Assert.IsNotNull(weibo.User);
                Assert.AreNotEqual(0, weibo.User.Id);
                Console.WriteLine("wid:{0} {1} uid:{2} says {3}", weibo.Id, weibo.User.ScreenName, weibo.User.Id, weibo.Text);
            }
        }
    }
}
